package com.walker.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.walker.entity.Author;
import com.walker.services.AuthorService;

import net.sf.json.JSONObject;

/**
 * 
 * Restful API
 * 
 * 作者： Walker
 *
 * 时间：2017年8月21日
 */
@RestController
@RequestMapping("/walker")
public class AuthorController {

	/**
	 * 引入apache log
	 */
	private static final Log log = LogFactory.getLog(AuthorController.class);

	@Autowired
	private AuthorService authorService;

	/**
	 * 查询用户列表
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> getAuthorList(HttpServletRequest request) {
		log.debug("进入查询用户列表");
		List<Author> authorList = authorService.findAuthorList();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("total", authorList.size());
		param.put("rows", authorList);
		return param;
	}

	/**
	 * 查询用户信息
	 */
	@RequestMapping(value = "/{userId:\\d+}", method = RequestMethod.GET)
	public Author getAuthor(@PathVariable Long userId, HttpServletRequest request) {
		Author author = this.authorService.findAuthor(userId);
		if (author == null) {
			throw new RuntimeException("查询错误");
		}
		return author;
	}

	/**
	 * 新增方法
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Integer add(@RequestBody JSONObject jsonObject) {
		String userId = jsonObject.getString("user_id");
		String realName = jsonObject.getString("real_name");
		String nickName = jsonObject.getString("nick_name");
		Author author = new Author();
		if (author != null) {
			author.setId(Long.valueOf(userId));
		}
		author.setRealName(realName);
		author.setNickName(nickName);
		try {
			return this.authorService.add(author);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("新增错误");
		}
	}

	/**
	 * 更新方法
	 */
	@RequestMapping(value = "/{userId:\\d+}", method = RequestMethod.POST)
	public void update(@PathVariable Long userId, @RequestBody JSONObject jsonObject) {
		Author author = this.authorService.findAuthor(userId);
		String realName = jsonObject.getString("real_name");
		String nickName = jsonObject.getString("nick_name");
		author.setRealName(realName);
		author.setNickName(nickName);
		try {
			this.authorService.update(author);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("更新错误");
		}
	}

	/**
	 * 删除方法
	 */
	@RequestMapping(value = "/{userId:\\d+}", method = RequestMethod.DELETE)
	public void delete(@PathVariable Long userId) {
		try {
			this.authorService.delete(userId);
		} catch (Exception e) {
			throw new RuntimeException("删除错误");
		}
	}
}
